package ui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import util.CapsJdbc;
import util.CapsRes;
import util.CapsTableView;
import util.ExtCheckBox;

import java.net.URL;
import java.util.*;

public class StuLessonMgr implements Initializable {

   @FXML
   private ListView<ExtCheckBox> lessonLv;

   @FXML
   private TableView<Map<String,String>> stuLessonTv;

   @FXML
   private Label stuName;

   private String sno;

   private List<String> selectedLessonList;

   @FXML
   private void saveStuLesson(ActionEvent event) {
      if (null == sno) {
         return;
      }
      List<Object> params = new ArrayList<>();
      params.add(sno);
      String delSql = "delete from stu_lesson_rel where sno=?";
      CapsJdbc.execSQL(delSql,params);
      String addSql = "insert into stu_lesson_rel(sno,lno) values(?,?)";
      for (String lno : selectedLessonList) {
         params.add(lno);
         CapsJdbc.execSQL(addSql,params);
         params.remove(lno);
      }
      this.dealQry();
   }

   @Override
   public void initialize(URL url, ResourceBundle rb) {
      selectedLessonList = new ArrayList<>();
      this.dealQry();
   }

   private void dealQry() {
      String strSQL = "select a.sno,a.name as stu_name,b.lno as rel_val,c.lno," +
              "c.name as lname,c.credit from stu a left outer join " +
              " stu_lesson_rel b on a.sno=b.sno left outer join lesson c " +
              "on b.lno=c.lno";
      CapsRes capsRes = CapsJdbc.execSQL(strSQL,null);
      CapsTableView.dealTableView(capsRes,stuLessonTv);
      CapsTableView.addButtonToTable(stuLessonTv,(recMap,idCol) -> {
         String delSql = "delete from stu_lession_rel where sno=? and lno = ?";
         List<Object> params = new ArrayList<>(2);
         params.add(recMap.get("sno"));
         params.add(recMap.get("lno"));
         CapsJdbc.execSQL(delSql,params);
         this.dealQry();
      },(recMap,idCol) -> {
         stuName.setText("要排课的学生："+recMap.get("stu_name"));
         sno = recMap.get("sno");
         String tmpSql = strSQL + " where a.sno='" + sno+"'";
         List<ExtCheckBox> extCheckBoxList = new ArrayList<>();
         dealLessonList(tmpSql, extCheckBoxList);
         tmpSql = "select a.lno,a.name as lname,c.sno as rel_val from lesson a " +
                 "left outer join stu_lesson_rel b on a.lno=b.lno left outer join" +
                 " stu c on b.sno=c.sno where c.sno is null";
         dealLessonList(tmpSql, extCheckBoxList);
         if (extCheckBoxList.size() > 0) {
            CapsTableView.dealListView(extCheckBoxList,lessonLv);
         }
      });
   }

   private void dealLessonList(String tmpSql,List<ExtCheckBox> extCheckBoxList) {
      CapsRes lessonRes = CapsJdbc.execSQL(tmpSql,null);

      for (List<String> rec : lessonRes.getRs()) {
         int flds = rec.size();
         ExtCheckBox extCheckBox = new ExtCheckBox();
         for (int i = 0; i < flds; i++) {
            if (rec.get(i) != null) {
               switch (lessonRes.getFlds().get(i)) {
                  case "lname":
                     extCheckBox.setText(rec.get(i));
                     break;
                  case "lno":
                     extCheckBox.setCode(rec.get(i));
                     break;
                  case "rel_val":
                     if (rec.get(i) != null) {
                        extCheckBox.setSelected(true);
                     }
                     break;
                  default:
                     break;
               }
            }
         }
         if (extCheckBox != null && extCheckBox.getCode() != null) {
            if (extCheckBox.isSelected()) {
               selectedLessonList.add(extCheckBox.getCode());
            }
            extCheckBox.setOnAction(e -> {
               ExtCheckBox chkbox = (ExtCheckBox) e.getSource();
               if (chkbox.isSelected()) {
                  selectedLessonList.add(chkbox.getCode());
               } else {
                  selectedLessonList.remove(chkbox.getCode());
               }
               System.out.println(selectedLessonList);
            });
            extCheckBoxList.add(extCheckBox);
         }
      }
   }
}
